package com.xm.httpapi.Aspect;

import android.app.Activity;

import com.xm.httpapi.Annotation.ViewL;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * Author:guohaijin
 * Date:
 * Description:
 */
public class ViewInjectUtils {
    //设置布局
    public static void injectContentView(Activity activity) {
        Class a = activity.getClass();
        if (a.isAnnotationPresent(ViewL.class)) {
            // 得到activity这个类的ContentView注解
            ViewL contentView = (ViewL) a.getAnnotation(ViewL.class);
            // 得到注解的值
            int layoutId = contentView.value();
            // 使用反射调用setContentView
            try {
                Method method = a.getMethod("setContentView", int.class);
                method.setAccessible(true);
                method.invoke(activity, layoutId);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    //获取布局
    public static int injectLayout(Activity activity) {
        Class a = activity.getClass();
        if (a.isAnnotationPresent(ViewL.class)) {
            // 得到activity这个类的ContentView注解
            ViewL contentView = (ViewL) a.getAnnotation(ViewL.class);
            // 得到注解的值
            return contentView.value();
        }
        return 0;
    }
}
